﻿using Newtonsoft.Json;
using StationCommunication;
using System;
using System.ComponentModel;

namespace ModuleUnit
{
    public class Config_SNIn
    {
        [Category("工站参数"), DisplayName("1.Ip地址 "), Description("Ip地址 ")]
        public string str_Ip地址 { set; get; } = "";
        [Category("工站参数"), DisplayName("2.Port "), Description("Port ")]
        public int n_Port { set; get; } = 8888;
    }
    [Description("接收SN")]
    public class Module_接收SN : ModuleBase
    {
        Load_Interface Load_Client = LoadFactory.Mark(LoadType.Client);
        public Config_SNIn config = null;
        public Module_接收SN(object config, string strName) : base( strName)
        {
            if (config == null)
            {
                this.config = new Config_SNIn();
            }
            else
            {
                this.config = JsonConvert.DeserializeObject<Config_SNIn>(config.ToString());

            }
            this.obj_Config = this.config;
        }
        public override bool Init()
        {
            if (config.str_Ip地址 != "" && config.n_Port > 0)
                Load_Client.Run(config.str_Ip地址, config.n_Port);
            return true;
        }

        public bool Function_接收SN(ref string strSN)
        {
            Retry:
            if (!Load_Client.IsLink())
            {
                goto Retry;
            }
            ResultData result = new ResultData();
            bool b_OK = false;
            Load_Client.SetStatus(DateTime.Now, null, "", "");
            Load_Client.GetStatusWait(out result,out b_OK);
            
            if (result == null)
                return false;
            else
            {
                strSN = result.Sn;
                return true;
            }

        }
        public bool Function_IsLink()
        {
            return Load_Client.IsLink();
        }

    }
}